// define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'addtabs', 'echarts', 'echarts-theme', 'echarts-macarons', 'template'], function ($, undefined, Backend,Table, Form, Datatable,Echarts, undefined, Template) {

    var width = document.body.clientWidth-110;
    var body_width = document.body.clientWidth/2.3;
    var Controller = {
        index: function () {
            Controller.consulting_statistics([],'consulting_history/consulting_statistics','num_consulting');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'consulting_history/index' + location.search,
                    // add_url: 'consulting_history/add',
                    // edit_url: 'consulting_history/edit',
                    // del_url: 'consulting_history/del',
                    multi_url: 'consulting_history/multi',
                    import_url: 'consulting_history/import',
                    table: 'consulting_history',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormTemplate: 'customformtpl',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                showExport:true,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                exportOptions: {
                    fileName: '資訊分類數據_' + Moment().format("YYYY-MM-DD"),  //导出名称
                    ignoreColumn: ['avatar'], //不导出字段
                    ignoreRow: [],
                    mso:{
                        // fileFormat:        'xlsx',
                        //修复导出数字不显示为科学计数法
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text())?'\\@':'';
                        }
                    },
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {field: 'name', title: '分類名稱'},
                        {field: 'all_num', title: '諮詢次數'},
                    ]
                ]
            });

            table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on("click", ".btn-success", function () {
                var params = $("form").serializeArray();
                var params = JSON.stringify (params);
                Controller.consulting_statistics(params,'consulting_history/consulting_statistics','num_consulting');
            });
        },
        // 積分充值金額統計
        integral_recharge_index: function () {
            Controller.consulting_statistics([],'consulting_history/integral_recharge_statistics','num_consulting');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'consulting_history/integral_recharge_index' + location.search,
                    // add_url: 'consulting_history/add',
                    // edit_url: 'consulting_history/edit',
                    // del_url: 'consulting_history/del',
                    multi_url: 'consulting_history/multi',
                    import_url: 'consulting_history/import',
                    table: 'consulting_history',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormTemplate: 'customformtpl',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                showExport:true,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                exportOptions: {
                    fileName: '積分充值金額統計數據_' + Moment().format("YYYY-MM-DD"),  //导出名称
                    ignoreColumn: [''], //不导出字段
                    ignoreRow: [],
                    mso:{
                        // fileFormat:        'xlsx',
                        //修复导出数字不显示为科学计数法
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text())?'\\@':'';
                        }
                    },
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {field: 'day', title: '日期'},
                        {field: 'payment_amount', title: '充值金額'},
                    ]
                ]
            });

            table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on("click", ".btn-success", function () {
                var params = $("form").serializeArray();
                var params = JSON.stringify (params);
                Controller.consulting_statistics(params,'consulting_history/integral_recharge_statistics','num_consulting');
            });
        },
        // 提現金額統計
        withdrawal_index: function () {
            Controller.consulting_statistics([],'consulting_history/withdrawal_statistics','num_consulting');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'consulting_history/withdrawal_index' + location.search,
                    // add_url: 'consulting_history/add',
                    // edit_url: 'consulting_history/edit',
                    // del_url: 'consulting_history/del',
                    multi_url: 'consulting_history/multi',
                    import_url: 'consulting_history/import',
                    table: 'consulting_history',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormTemplate: 'customformtpl',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                showExport:true,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                exportOptions: {
                    fileName: '提現金額統計數據_' + Moment().format("YYYY-MM-DD"),  //导出名称
                    ignoreColumn: [''], //不导出字段
                    ignoreRow: [],
                    mso:{
                        // fileFormat:        'xlsx',
                        //修复导出数字不显示为科学计数法
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text())?'\\@':'';
                        }
                    },
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {field: 'day', title: '日期'},
                        {field: 'money', title: '提現金額'},
                    ]
                ]
            });

            table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on("click", ".btn-success", function () {
                var params = $("form").serializeArray();
                var params = JSON.stringify (params);
                Controller.consulting_statistics(params,'consulting_history/withdrawal_statistics','num_consulting');
            });
        },
        // 交易金額統計
        transaction_index: function () {
            Controller.consulting_statistics([],'consulting_history/transaction_statistics','num_consulting');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'consulting_history/transaction_index' + location.search,
                    // add_url: 'consulting_history/add',
                    // edit_url: 'consulting_history/edit',
                    // del_url: 'consulting_history/del',
                    multi_url: 'consulting_history/multi',
                    import_url: 'consulting_history/import',
                    table: 'consulting_history',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormTemplate: 'customformtpl',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                showExport:true,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                exportOptions: {
                    fileName: '交易訂單金額統計數據_' + Moment().format("YYYY-MM-DD"),  //导出名称
                    ignoreColumn: [''], //不导出字段
                    ignoreRow: [],
                    mso:{
                        // fileFormat:        'xlsx',
                        //修复导出数字不显示为科学计数法
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text())?'\\@':'';
                        }
                    },
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {field: 'day', title: '日期'},
                        {field: 'money', title: '支付金額'},
                    ]
                ]
            });

            table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on("click", ".btn-success", function () {
                var params = $("form").serializeArray();
                var params = JSON.stringify (params);
                Controller.consulting_statistics(params,'consulting_history/information_access_statistics','num_consulting');
            });
        },
        // 資訊訪問統計頁面
        information_access_index: function () {
            Controller.consulting_statistics([],'consulting_history/information_access_statistics','num_consulting');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'consulting_history/information_access_index' + location.search,
                    // add_url: 'consulting_history/add',
                    // edit_url: 'consulting_history/edit',
                    // del_url: 'consulting_history/del',
                    multi_url: 'consulting_history/multi',
                    import_url: 'consulting_history/import',
                    table: 'consulting_history',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormTemplate: 'customformtpl',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                showExport:true,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                exportOptions: {
                    fileName: '資訊訪問統計數據_' + Moment().format("YYYY-MM-DD"),  //导出名称
                    ignoreColumn: ['operate'], //不导出字段
                    ignoreRow: [],
                    mso:{
                        // fileFormat:        'xlsx',
                        //修复导出数字不显示为科学计数法
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text())?'\\@':'';
                        }
                    },
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {field: 'name', title: '分類'},
                        {field: 'all_num', title: '訪問次數'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter:function(value,row,index){
                            this.buttons = [
                                        {
                                            name: 'staff_users',
                                            text: '資訊訪問詳情',
                                            classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                                            url: 'consulting_history/access_user_list/type/'+row.type,
                                            // confirm: '确认禁用嗎？',
                                            visible: function (row) {
                                                if ( Config.access_user_list) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true,
                                        }
                                    ];
                                return Table.api.formatter.operate.call(this,value,row,index);
                            }}
                    ]
                ]
            });

            table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on("click", ".btn-success", function () {
                var params = $("form").serializeArray();
                var params = JSON.stringify (params);
                Controller.consulting_statistics(params,'consulting_history/information_access_statistics','num_consulting');
            });
        },
        // 資訊訪問詳情
        access_user_list: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'consulting_history/access_user_list/ids/'+Config.ids+'/type/'+Config.type,
                    // add_url: 'user/user/add',
                    // edit_url: 'user/user/edit',
                    // del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                searchFormTemplate: 'customformtpl',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                // showExport:false,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                exportOptions: {
                    fileName: '資訊訪問詳情_' + Moment().format("YYYY-MM-DD"),  //导出名称
                    ignoreColumn: ['avatar'], //不导出字段
                    ignoreRow: [],
                    mso:{
                        // fileFormat:        'xlsx',
                        //修复导出数字不显示为科学计数法
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text())?'\\@':'';
                        }
                    },
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {field: 'id', title: '用戶ID', sortable: true},
                        {field: 'avatar', title: '用戶頭像', events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'nickname', title: '用戶名', operate: 'LIKE'},
                        {field: 'mobile', title: '用戶賬號', operate: 'LIKE'},
                        {field: 'region_address', title: '城市', operate: false},
                        {field: 'createtime', title: '訪問時間', operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });

            table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        // 数据统计-諮詢分類數據統計
        consulting_statistics:function(data,url,type){
            $.ajax({
                url: url,
                type: 'post',
                data: data,
                success: function (res) {

                            var labelOption = {
                                normal: {
                                    rotate: 90,
                                    align: 'left',
                                    verticalAlign: 'middle',
                                    position: 'insideBottom',
                                    distance: 15
                                }
                            };

                    var myChart = Echarts.init(document.getElementById(type), 'walden',{width: width});
                    var option = {
                        color: ['#1C86EE'],
                        title: {
                            text: res.title
                        },
                        calculable: true,
                        tooltip: {
                            trigger: 'axis',
                            axisPointer : {
                                type : 'shadow'
                            }
                        },
                        grid: {
                            left: '3%',
                            right: '4%',
                            bottom: '3%',
                            containLabel: true
                        },
                        xAxis: {
                            type: 'category',
                            axisTick: {
                                alignWithLabel: true
                            },
                            data: res.index_date
                        },
                        yAxis: {
                            type: 'value'
                        },
                        legend: {
                            data:[res.legend]
                        },
                        toolbox: {
                            feature: {
                                saveAsImage: {}
                            }
                        },
                        series: [
                            {
                                name:res.legend,
                                type:'bar',
                                barGap: 0,
                                label: labelOption,
                                stack: res.unit,
                                data:res.all_num
                            }
                        ]
                    };

                    myChart.setOption(option, true);

                }
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});